import 'dart:convert';

import 'package:app_flutter/beans/busssch_bean.dart';
import 'package:app_flutter/routes.dart';
import 'package:app_flutter/utils/mydio.dart';
import 'package:app_flutter/view/loading.dart';
import 'package:dio/dio.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';

import '../const.dart';

class ComPage extends StatefulWidget {
  final String type;

  ComPage({String type}) : this.type = type;

  @override
  _ComPageState createState() => _ComPageState(type: type);
}

class _ComPageState extends State<ComPage> with AutomaticKeepAliveClientMixin{
  String _type;
  var loadingDialog;
  List<Map<String, dynamic>> maps = List();
  List<BussschBean> items = List();

  _ComPageState({String type}) : this._type = type;

  List<Widget> doWidgets() {
    List<Widget> listTitles = List();
    for (BussschBean bean in items) {
      listTitles.add(buildListItem(context, bean));
    }
    return listTitles;
  }

  Widget buildListItem(BuildContext context, BussschBean bean) {
    return GestureDetector(
        onTap: () {
          print('inscode:${bean.inscode}');
          String route = '${Routes.comDetail}?inscode=${bean.inscode}';
          Routes.router.navigateTo(context, route,
              replace: false, transition: TransitionType.fadeIn);
        },
        child: Card(
          elevation: 0,
          color: Colors.transparent,
          child: Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          ImageIcon(
                            AssetImage('images/icon_school.png'),
                            color: Color(0xff23AFDC),
                          ),
                          SizedBox(
                            width: 5,
                          ),
                          Text(bean.shortname,
                              style: TextStyle(
                                  color: Color(0xff333333), fontSize: 14))
                        ],
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          ImageIcon(
                            AssetImage('images/icon_dizhi.png'),
                            color: Color(0xff23AFDC),
                          ),
                          SizedBox(
                            width: 5,
                          ),
                          SizedBox(
                            width: 280,
                            child: Text(bean.address,
                                style: TextStyle(
                                    color: Color(0xff666666), fontSize: 12)),
                          )
                        ],
                      ),
                    ],
                  ),
                  Text(bean.busistatus == '1' ? '营业' : '非营业',
                      style: TextStyle(color: Color(0xff23AFDC), fontSize: 12))
                ],
              ),
            ],
          ),
        ));
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _list(_type);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.fromLTRB(15, 10, 15, 10),
        child: ListView.separated(
          shrinkWrap: true,
          itemBuilder: (context, index) => doWidgets()[index],
          separatorBuilder: (context, index) => Divider(
            color: Color(0xffF0F0F0),
          ),
          itemCount: doWidgets().length,
        ));
  }

  _showDialog() {
    loadingDialog = LoadingDialog(
      text: '加载中...',
    );
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return loadingDialog;
        });
  }

  _list(String type) {
    Map data = Map<String, dynamic>();
    data["level"] = '';
    data["name"] = '';
    data["organizId"] = '';
    data["sonOrganizId"] = '';
    data["status"] = type;
    MyDio.getInstance().post('/api/bussSch/list', (data) {
      print(data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }
      items.clear();
      for (var s in data) {
        items.add(BussschBean.fromJson(s));
      }
      setState(() {});
    }, params: data);
//    print('type:$type');
//    try {
//      // flutter packages pub run build_runner build
//      Dio dio = Dio(Const.post);
//      print(dio.options.method);
//      Response response =
//          await dio.request('/api/bussSch/list', queryParameters: data);
//
//      if (loadingDialog != null) {
//        loadingDialog = null;
//        Navigator.pop(context);
//      }
//
//      Map<String, dynamic> decoded = json.decode(response.toString());
//      print('response:$response');
//      BussschBean onelistBean = BussschBean.fromJson(decoded);
//      setState(() {
//        items = onelistBean.data;
//      });
//    } catch (e) {
//      print(e);
//    }
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
